package cn.bluethink.eguan.module.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.bluethink.eguan.core.security.Token;
import cn.bluethink.eguan.core.service.EgTavernService;
import cn.bluethink.eguan.core.service.EgUserService;
import cn.bluethink.eguan.filter.NameFilter;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.model.core.EgPurview;
import cn.bluethink.eguan.model.core.EgTavern;
import cn.bluethink.eguan.model.core.EgUser;
import cn.bluethink.eguan.model.module.EgModule;
import cn.bluethink.eguan.model.module.EgModuleObject;
import cn.bluethink.eguan.module.entity.EgModuleEntity;
import cn.bluethink.eguan.module.service.EgModuleService;
import cn.bluethink.eguan.result.ResponseMessage;
import cn.bluethink.eguan.result.ResponseResult;

/**
 * 驿馆+服务
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.fanbl于2018/3/11设计并构建初始版本
 */
@CrossOrigin
@RestController
@RequestMapping("/eguan/v1.0/module")
public class EgModuleController {

	@Autowired
	HttpServletRequest request;

	@Autowired
	EgModuleService moduleService;

	@Autowired
	EgUserService userService;

	@Autowired
	EgTavernService tavernService;

	/**
	 * 创建服务
	 * 
	 * @param module
	 * @return
	 * @throws Exception
	 */
	@Token(purviews = { EgPurview.SERVICE_MG })
	@PostMapping("/create")
	public ResponseResult<EgModule> create(@RequestBody  EgModule module) throws Exception {
		if(module.getSummary() == null || module.getSummary() == "")
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "服务概述不能为空！");
		if(module.getDes() == null || module.getDes()  == "")
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "服务详情不能为空！");
		if(module.getName() == null || module.getName() == "")
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "服务名称不能为空！");
		if(module.getIcon() == null || module.getIcon() == "")
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "服务图标不能为空！");
		
		// 设置创建服务者
	    Long uid = (Long) request.getAttribute("userId");
		// 创建服务
		int create = moduleService.create(module, uid);
		if (create > 0)
			return new ResponseResult<>(ResponseMessage.OK);
		return new ResponseResult<>(ResponseMessage.CREATE_FAIL);

	}

	/**
	 * 更新已创建的服务
	 * 
	 * @param module
	 * @return
	 * @throws Exception
	 */
    @Token(purviews = { EgPurview.SERVICE_MG })
	@PostMapping("/update")
	public ResponseResult<EgModule> update(@RequestBody @Validated EgModule module) throws Exception {
		// 更新服务
		int update = moduleService.update(module);
		if (update > 0)
			return new ResponseResult<>(ResponseMessage.OK);
		if (update == 0)
			return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "该服务不存在！");
		return new ResponseResult<>(ResponseMessage.UPDATE_FAIL);

	}

	/**
	 * 开通者更新已开通的服务
	 * 
	 * @param module
	 * @return
	 * @throws Exception
	 */
	@Token(purviews = { EgPurview.SERVICE_MG })
	@PostMapping("/updateOpenServe")
	public ResponseResult<EgModuleObject> updateOpenServe(@RequestBody  EgModuleObject module)
			throws Exception {
		if(module.getModule() == null)
	        	return new ResponseResult<>(ResponseMessage.NOTNULL_ID.getStatus(), "请求服务不能为空！");
        if(module.getModule().getId() == null)
        	return new ResponseResult<>(ResponseMessage.NOTNULL_ID.getStatus(), "请求id不能为空！");
        if(module.getFaction() == null )
        	return new ResponseResult<>(ResponseMessage.NOTNULL_ID.getStatus(), "门派不能为空！");
        if(module.getFaction().getId() == null )
        	return new ResponseResult<>(ResponseMessage.NOTNULL_ID.getStatus(), "门派id不能为空！");
        
		int resule = moduleService.updateOpenServe(module);
		 
		if (resule == 0)
			return new ResponseResult<>(ResponseMessage.UPDATE_FAIL.getStatus(), "找不到该服务或该服务未开通！");
		if (resule > 0)
			return new ResponseResult<>(ResponseMessage.OK);
		return new ResponseResult<>(ResponseMessage.UPDATE_FAIL);

	}

	/**
	 * 删除服务
	 * 
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	@Token(purviews = { EgPurview.SERVICE_MG })
	@PostMapping("/del")
	public ResponseResult<Long> del(Long mid) throws Exception {
		if (mid == null)
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "请求参数不能为空！");
		// 已经开通的服务不能删除
		boolean bOpen = moduleService.bOpen(mid);
		if (bOpen)
			return new ResponseResult<>(ResponseMessage.DELETE_FAIL.getStatus(), "该服务已经开通！无法删除");
		int del = moduleService.del(mid);
		if (del > 0)
			return new ResponseResult<>(ResponseMessage.OK);
		if (del == 0)
			return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "该服务不存在！");
		return new ResponseResult<>(ResponseMessage.DELETE_FAIL);

	}

	/**
	 * 服务展示
	 * 
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/query")
	public ResponseResult<List<EgModule>> query() throws Exception {
		// 将已经创建好的服务全部展示出来
		List<EgModule> modules = moduleService.query();
		return new ResponseResult<>(ResponseMessage.OK, modules);

	}

	/**
	 * 服务开通
	 * 
	 * @param moduleObject
	 * @return
	 * @throws Exception
	 */
	@Token(purviews = { EgPurview.FACTION_MG })
	@PostMapping("/open")
	public ResponseResult<Long> open(@RequestBody EgModuleObject moduleObject) throws Exception {
		Long uid = (Long) request.getAttribute("userId");
		if (moduleObject == null)
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "请求参数不能为空！");
		if (moduleObject.getFaction().getId() == null)
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "门派不能为空！");
		Long fid = moduleObject.getFaction().getId();
		int open = moduleService.open(moduleObject, uid, fid);
		if (open == 0)
			return new ResponseResult<>(ResponseMessage.OPEN_FAIL.getStatus(), "该服务已经开通！");
		if (open > 0)
			return new ResponseResult<>(ResponseMessage.OK);
		return new ResponseResult<>(ResponseMessage.OPEN_FAIL);

	}

	/**
	 * 服务关闭
	 * 
	 * @param filter
	 * @return
	 * @throws Exception
	 */
	@Token(purviews = { EgPurview.FACTION_MG })
	@PostMapping("/close")
	public ResponseResult<Long> close(@RequestBody EgModuleObject moduleObject) throws Exception {
		if (moduleObject == null)
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "请求参数不能为空！");
		if (moduleObject.getModule().getId() == null)
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "服务id不能为空！");
		if (moduleObject.getFaction().getId() == null)
			return new ResponseResult<>(ResponseMessage.NULL_REQUEST.getStatus(), "门派id不能为空！");
		Long mid = moduleObject.getModule().getId();
		Long fid = moduleObject.getFaction().getId();
		// 删除eg_moduleobject
		int close = moduleService.close(mid, fid);
		if (close == 0)
			return new ResponseResult<>(ResponseMessage.DATA_NOT_FOUND.getStatus(), "该服务不存在！");
		 
		if (close > 0)
			return new ResponseResult<>(ResponseMessage.OK);
		return new ResponseResult<>(ResponseMessage.CLOSE_FAIL);

	}

}
